﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Web.Core;
using mesoBoard.Data;
using System.IO;

namespace mesoBoard.Web.Areas.Admin.Controllers
{
    public class FileTypesController : mesoBoardAdminController
    {
        public ActionResult FileTypes()
        {
            IEnumerable<FileType> FileTypes = Repositories.FileTypes.GetFileTypes();
            DirectoryInfo fileTypeImagesDirectory = new DirectoryInfo(Server.MapPath("~/Images/FileTypes"));
            List<FileInfo> fileTypeImages = new List<FileInfo>();
            string[] fileExtensions = { ".png", ".gif" };

            foreach (string e in fileExtensions)
            {
                fileTypeImages.AddRange(fileTypeImagesDirectory.GetFiles("*" + e).ToList());
            }

            ViewData["Image"] = new SelectList(fileTypeImages, "Name", "Name");
            return View(FileTypes);
        }

        public ActionResult CreateFileType(FileType TheFileType)
        {
            if (string.IsNullOrWhiteSpace(TheFileType.Extension))
                SetError("Enter a file extension");
            else if (!TheFileType.Extension.Contains("."))
                SetError("Enter a valid file extension");
            else
            {
                Repositories.FileTypes.CreateFileType(TheFileType);
                SetSuccess("File type created");
            }

            return RedirectToAction("FileTypes");
        }

        public ActionResult DeleteFileType(int FileTypeID)
        {
            Repositories.FileTypes.DeleteFileType(FileTypeID);
            SetSuccess("File type deleted");
            return RedirectToAction("FileTypes");
        }

        [HttpGet]
        public ActionResult EditFileType(int FileTypeID)
        {
            List<FileInfo> fileTypeImages = mbServices.FileTypes.GetFileTypeImages();

            FileType ft = Repositories.FileTypes.GetFileType(FileTypeID);

            ViewData["Image"] = new SelectList(fileTypeImages, "Name", "Name", ft.Image);

            return View(ft);
        }

        [HttpPost]
        public ActionResult EditFileType(FileType ft)
        {
            SetCrumb("File Types");


            List<FileInfo> fileTypeImages = mbServices.FileTypes.GetFileTypeImages();

            ViewData["Image"] = new SelectList(fileTypeImages, "Name", "Name");

            if (string.IsNullOrEmpty(ft.Extension))
                ModelState.AddModelError("Extension", "Enter an extension");
            else if (!ft.Extension.Contains("."))
                ModelState.AddModelError("Extension", "Enter a valid extension");

            if (ModelState.IsValid)
            {
                SetSuccess("File type edited");
                Repositories.FileTypes.SaveFileType(ft);
                return RedirectToAction("FileTypes");
            }
            else
            {
                SetError("File type validation error");
                return View(ft);
            }
        }
    }
}
